import Vue from 'vue'
import VueRouter from 'vue-router'
import store from "@/store";
import { CacheUtils } from '../utils/cacheUtils.js';
Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/Register.vue')
  },
  {
    path: '/404',
    name: '404',
    component: () => import('../views/404.vue')
  },
    /*用户前台页面*/
  {
    path: '/front',
    name: 'Front',
    component: () => import('../views/front/Front'),
    children: [
      {
        path: 'home',
        name: 'FrontHome',
        component: () => import('../views/front/Home.vue')
      },
      {
        path: 'item1',
        name: 'Item1',
        component: () => import('../views/front/Item1.vue')
      },
      {
        path: 'person',
        name: 'FrontPerson',
        component: () => import('../views/front/Person')
      },
      {
        path: 'password',
        name: 'FrontPassword',
        component: () => import('../views/front/Password')
      },
      {
        path: 'video',
        name: 'Video',
        component: () => import('../views/front/Video')
      },
      {
        path: 'videoDetail',
        name: 'VideoDetail',
        component: () => import('../views/front/VideoDetail')
      },
      {
        path: 'article',
        name: 'FrontArticle',
        component: () => import('../views/front/Article')
      },
      {
        path: 'articleDetail',
        name: 'ArticleDetail',
        component: () => import('../views/front/ArticleDetail')
      },
    ]
  },
  { path: '*', redirect: '/404'}
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// const createRouter = () => new Router({
//   // mode: 'history', // require service support
//   scrollBehavior: () => ({ y: 0 }),
//   routes: constantRoutes
// })
//
// const router = createRouter()

// 提供一个重置路由的方法
export const resetRouter = () => {
  router.matcher = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
  })
}

// 注意：刷新页面会导致页面路由重置
export const setRoutes = () => {
  const storeMenus = localStorage.getItem("menus"); // 获取 menus
  if (storeMenus) {// storeMenus有值
    // 获取当前的路由对象名称数组
    const currentRouteNames = router.getRoutes().map(v => v.name)
    // 路由里存不存在Manage(第一次是不存在的)
    if (!currentRouteNames.includes('Manage')) {
      // 拼装动态路由
      const manageRoute = {
            path: '/',
            name: 'Manage',
            component: () => import('../views/Manage.vue'), redirect: "/home",
            children: [
                { path: 'person', name: '个人信息', component: () => import('../views/Person.vue')},
                { path: 'password', name: '修改密码', component: () => import('../views/Password.vue')}
             ]
          }
      const menus = JSON.parse(storeMenus) // 转成对象
      console.log("menus",menus)
      menus.forEach(item => {
        if (item.path) {  // 当且仅当path不为空的时候才去设置路由
          let itemMenu = {
                path: item.path.replace("/", ""), // 去除 /xxx的 /
                name: item.name,
                component: () => import('../views/' + item.pagePath + '.vue')
              }
          manageRoute.children.push(itemMenu)
        } else if(item.children.length>0) { // 有children的item的第一级的path是null
          item.children.forEach(item => {
            if (item.path) {
              let itemMenu = {
                path: item.path.replace("/", ""),
                name: item.name,
                component: () => import('../views/' + item.pagePath + '.vue')}
              manageRoute.children.push(itemMenu)
            }
          })
        }
      })
      // 动态添加到现在的路由对象中去
      console.log(" router.addRoute:",manageRoute)
      router.addRoute(manageRoute)

    }

  }
}

// 页面刷新的时候,我就再set一次路由
setRoutes()

//
// router.beforeEach((to, from, next) => {
//   // 切换路由清除缓存
//   CacheUtils.clearCache();
//   // 网页卸载前清除缓存
//   window.addEventListener('beforeunload', () => CacheUtils.clearCache(), false);
//
//   localStorage.setItem("currentPathName", to.name)  // 设置当前的路由名称
//   store.commit("setPath") // 汉堡包那里用到的
//   console.log("to",to)
//   // 未找到路由的情况  ==> 跳转的路由是没有的
//   if (!to.matched.length) {
//     const storeMenus = localStorage.getItem("menus") // 去获取menus
//     if (storeMenus) { // 就是已登录
//       next("/404")
//     } else {
//       // 跳回登录页面
//       next("/login")
//     }
//   }
//   // 其他的情况都放行
//   next()
//
// })

export default router
